import { request } from '../../request/index';
Page({
  data: {
    tabs:[
      {
        id: 0,
        value:'全部',
        isActive: true
      },
      {
        id: 1,
        value:'待付款',
        isActive: false
      },
      {
        id: 2,
        value:'代发货',
        isActive: false
      },
      {
        id: 3,
        value:'退货/退款',
        isActive: false
      }
    ],
  },
  onShow(options) {
    //获取当前小程序的页面栈，
    let pages = getCurrentPages();
    //数组中索引最大的页面就是当前页面
    let currentPage = pages[pages.length - 1];
    console.log(currentPage.options);
    //获取url上的type参数
    const { type } = currentPage.options;
    //激活选中标题
    this.changeTitleByIndex(type-1);
    this.getOrders(type);
  },
  async getOrders(type) {
    const token = wx.getStorageSync('token');
    if(!token) {
      wx.navigateTo({
        url: '/pages/auth/index',
      })
      return;
    }
    const res = await request({url:'/my/orders/all',data:{type}});
    console.log(res)
  },
  //根据点击来决定标题是否选中
  changeTitleByIndex(index) {
    //改变当前的值
    let { tabs } = this.data;
    tabs.forEach((v,i)=> i===index?v.isActive=true:v.isActive=false);
    this.setData({
      tabs
    })
  },
  handleTabsItemChange(e) {
    //获取到index
    let {index} = e.detail;
    this.changeTitleByIndex(index);
    //重新发送请求
    this.getOrders(index+1);
  },
})